package com.aiti.lulian.utils;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.config.RegistryBuilder;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.ssl.SSLContexts;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import javax.net.ssl.SSLContext;
import java.io.InputStream;

public class HttpClientUtil {

    private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36";

    public static Document getDocumentFromURL(String url) {
        try {
            // 创建信任所有证书的 SSLContext
            SSLContext sslContext = SSLContexts.custom()
                    .loadTrustMaterial((chain, authType) -> true)
                    .build();

            // 创建连接工厂
            ConnectionSocketFactory plainSocketFactory = PlainConnectionSocketFactory.getSocketFactory();
            SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
                    sslContext,
                    NoopHostnameVerifier.INSTANCE
            );

            // 构建 HttpClient
            try (CloseableHttpClient httpClient = HttpClients.custom().setUserAgent(USER_AGENT)
                    .setConnectionManager(new PoolingHttpClientConnectionManager(
                            RegistryBuilder.<ConnectionSocketFactory>create()
                                    .register("http", plainSocketFactory)
                                    .register("https", sslSocketFactory)
                                    .build()
                    ))
                    .build()) {

                HttpGet request = new HttpGet(url);
                request.setHeader("User-Agent", "Mozilla/5.0");
                request.setHeader("Referer", "https://www.baidu.com/");
                request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");


                ClassicHttpResponse response = httpClient.executeOpen(null, request, null);
                InputStream content = response.getEntity().getContent();

                return Jsoup.parse(content, null, url);
            }

        } catch (Exception e) {
            System.err.println("获取HTML文档失败：" + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    // 测试主方法
    public static void main(String[] args) {
        String testUrl = "https://www.baidu.com"; // 可替换为目标网站
        Document doc = getDocumentFromURL(testUrl);
        if (doc != null) {
            System.out.println("页面标题: " + doc.title());
        } else {
            System.out.println("未能成功获取页面");
        }
    }
}